package com.sun.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sun.common.Exception.CustomException;
import com.sun.entity.DrugSupplier;
import com.sun.entity.Supplier;
import com.sun.mapper.SupplierMapper;
import com.sun.service.DrugSupplierService;
import com.sun.service.SupplierService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Project: hospitalDrugInformationManagement
 * @Author: 86178
 * @Date: 2023/2/21 17:09
 * @FileName: SupplierServiceImpl
 * @Description: ""
 */
@Service
public class SupplierServiceImpl extends ServiceImpl<SupplierMapper, Supplier> implements SupplierService {
    @Autowired
    DrugSupplierService drugSupplierService;

    @Autowired
    SupplierMapper supplierMapper;

    @Override
    public boolean remove(Long id) {
        LambdaQueryWrapper<DrugSupplier> dishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        //添加查询条件，根据分类id进行查询
        dishLambdaQueryWrapper.eq(DrugSupplier::getSupplierId, id);
        int count = drugSupplierService.count(dishLambdaQueryWrapper);

        //查询当前分类是否关联了药品，如果已经关联，抛出一个业务异常
        if (count > 0) {
            //已经关联药品，抛出一个业务异常
            throw new CustomException("当前供应商关联了药品，不能删除");
        }
        //正常删除分类
        return super.removeById(id);
    }

    @Override
    public Supplier selectByName(String name) {
        LambdaQueryWrapper<Supplier> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Supplier::getSupplierName, name);
        Supplier supplier = supplierMapper.selectOne(lambdaQueryWrapper);
        return supplier;
    }
}
